package com.huahua.api.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.huahua.api.base.R;
import com.huahua.api.entity.pojo.ApiHoliday;
import com.huahua.api.entity.pojo.ApiHolidayCountries;
import com.huahua.api.entity.req.holiday.GetHolidayListParam;
import com.huahua.api.entity.vo.holiday.HolidayDetailVo;
import com.huahua.api.service.ApiCountriesService;
import com.huahua.api.service.ApiHolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author LoveHuahua
 * @date 2021年09月07日 13:26
 * @description believe in yourself
 */
@RestController
@RequestMapping("holiday")
public class HolidayApiController {

    @Autowired
    ApiHolidayService apiHolidayService;

    @Autowired
    ApiCountriesService apiCountriesService;

    /**
     * 查询单个国家所有的节日
     */
    @RequestMapping("getHoliday")
    private R<HolidayDetailVo> getHoliday(@RequestBody HolidayDetailVo holidayDetailVo) {
        //节假日
        List<ApiHoliday> holidays = apiHolidayService.lambdaQuery().eq(ApiHoliday::getCountry, holidayDetailVo.getCountryId()).eq(holidayDetailVo.getYear() != null ,ApiHoliday::getYear,holidayDetailVo.getYear()).list();
        //国家
        ApiHolidayCountries countries = apiCountriesService.lambdaQuery().eq(ApiHolidayCountries::getId, holidayDetailVo.getCountryId()).one();
        //设置国家名
        holidayDetailVo.setCountryName(countries.getName());
        holidayDetailVo.setData(holidays);
        return R.success(holidayDetailVo);
    }



    /**
     * 查询所有国家的所有节日
     */
    @RequestMapping("getHolidayList")
    private R<List<HolidayDetailVo>> getHolidayList(@RequestBody GetHolidayListParam param) {
        ArrayList<HolidayDetailVo> objects = new ArrayList<>();
        List<ApiHolidayCountries> list = apiCountriesService.lambdaQuery().list();
        for (ApiHolidayCountries apiHolidayCountries : list) {
            HolidayDetailVo holidayDetailVo = new HolidayDetailVo();
            //节假日
            List<ApiHoliday> holidays = apiHolidayService.lambdaQuery()
                    .eq(param.getYear() != null, ApiHoliday::getYear, param.getYear())
                    .eq(param.getMonth() != null, ApiHoliday::getMonth, param.getMonth())
                    .eq(ApiHoliday::getCountry,apiHolidayCountries.getId())
                    .eq(param.getDay() != null, ApiHoliday::getDay, param.getDay())
                    .eq(param.getDate() != null, ApiHoliday::getHolidayTime, param.getDate())
                    .list();
            //设置国家名
            holidayDetailVo.setCountryName(apiHolidayCountries.getName());
            holidayDetailVo.setData(holidays);
            objects.add(holidayDetailVo);
        }
        return R.success(objects);
    }

}
